import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { UserEntity } from "./user.entity";
import { Repository } from "typeorm";
import { userInfo } from "os";
import { UserRegisterDto } from "./user.dto";

@Injectable()
export class UserDao {

  constructor(
    @InjectRepository(UserEntity) private readonly userRepository: Repository<UserEntity>
  ) {
  }

  async getUserAllByName(username: string): Promise<UserEntity> {
    return await this.userRepository.findOne({
      where: { username }
    });
  }

  async getUserInfoByName(username: string): Promise<UserEntity> {
    return await this.userRepository.findOne({
      select: ["id", "username", "email"],
      where: { username }
    });
  }

  async getUserByEmail(mail:string):Promise<UserEntity>{
    return await this.userRepository.findOne({
      select: ["id", "username"],
      where: { email:mail }
    })

  }

  async saveUserInfo(user: UserRegisterDto): Promise<UserEntity | null> {
    try {
      return await this.userRepository.save(user);
    } catch (e) {
      return null;
    }

  }

}
